import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/PromptAction.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
  // 社区公告列表
  List announceList = [];
  @override
  void initState() {
    super.initState();
    _getAnnounceList();
  }

  // 获取社区公告列表
  _getAnnounceList() async {
    final res = await getAnnounceListAPI();
    setState(() {
      announceList = res;
    });
  }

  // 定时器ID
  Timer? timer;
  // 时长
  int _duration = 60;

  // 获取测试按钮
  List<Widget> getBtnChild() {
    return [
      ElevatedButton(
          onPressed: () {
            tokenManager.setToken('12345');
          },
          child: const Text("设置token")),
      ElevatedButton(
          onPressed: () {
            // 开启定时器
            timer = Timer.periodic(const Duration(seconds: 1), (timer) {
              setState(() {
                _duration--;
              });
            });
          },
          child: const Text("开始定时器")),
      Text('时间是:$_duration'),
      ElevatedButton(
          onPressed: () {
            timer?.cancel();
          },
          child: const Text("关闭定时器")),
      ElevatedButton(
          onPressed: () {
            Future.delayed(const Duration(seconds: 3), () {
              Promptaction.showToast('延时3秒');
            });
          },
          child: const Text("延时器")),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(237, 240, 251, 1),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          Row(
            children: getBtnChild(),
          ),
          // 导航条
          HomeNav(),
          // 图片
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),
          // 社区公告
          HomeList(notifyList: announceList)
        ],
      ),
    );
  }
}
